<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Admin_Merchmanagement_Attributes extends Controller_Admin_Template {
	
	public function before() {
		parent::before();
		
		$this->template->title = __('Merchandise Management');
	}
	
	public function action_index() {
		$category_id = $this->request->param('id');
		$category = new Model_Category($category_id);
		
		$attributes = $category->attributes->find_all();
		
		$view = new View('admin/merchmanagement/attributes/index');
		$view->set('category', $category);
		$view->set('attributes', $attributes);
		
		$this->template->content = $view;
	}
	
	public function action_add() {
		$category_id = $this->request->param('id');
		$attribute = new Model_Attribute();
		$attribute->values($_POST);
		$attribute->category_id = $category_id;
		
		$attribute->save();
		$this->request->redirect('admin/merchmanagement/attributes/'.$category_id);
	}
	
	public function action_delete() {
		$attribute_id = $this->request->param('id');
		$attribute = new Model_Attribute($attribute_id);
		$category_id = $attribute->category->id;
		$values = $attribute->values->find_all();
		foreach($values as $value):
			$value->delete();
		endforeach;
		
		$attribute->delete();
		
		$this->request->redirect('admin/merchmanagement/attributes/'.$category_id);
	}
	
}